<?php


class CommentAction extends BaseAction {
    public function add(){
        $pId = isset($_POST['pid']) ? trim($_POST['pid']) : 0;
        $name = isset($_POST['name']) ? trim($_POST['name']) : '';
        $email = isset($_POST['email']) ? trim($_POST['email']) : '';
        $web = isset($_POST['web']) ? trim($_POST['web']) : '';
        $content = isset($_POST['content']) ? trim($_POST['content']) : '';
        if(0 == $pId || '' == $name || '' == $email || '' == $content || strlen($name) > 32 || strlen($email) > 32 || strlen($web) > 32 || strlen($content) > 255){
            $result['info'] = 'access denied！';
            $result['status'] = '500';
            $this->ajaxReturn($result);
        }

        $data = array(
            'level' => '1',
            'nickname' => $name,
            'email' => $email,
            'web_site' => $web,
            'level' => 0
            );
        $db = M('User');
        $count = $db->where("email = '$email'" )->count();
        
        if($count > 0){
            unset($data['level']);
            $db->where("email = '$email'" )->data($data)->save();
            $res = $db->where("email = '$email'" )->getField('id');
        }else{
            $res = $db->data($data)->add();	
        }
        unset($data);
        if($res){
            $data['u_id'] = $res;
            $data['p_id'] = $pId;
            $data['content'] = $content;
            $data['add_time'] = time();
            $data['is_verify'] = 0;
            $res = M('Comment')->data($data)->add();
            if($res){
                $result['info'] = '成功！';
                $result['status'] = '200';
                $this->ajaxReturn($result);
            }
        }else{
            $result['info'] = '评论失败！';
            $result['status'] = '500';
            $this->ajaxReturn($result);
        }	

        
    }
}